package cz.cuni.mff.ufal.volk;

import org.apache.log4j.Logger;

import cz.cuni.mff.ufal.volk.Listener;
import cz.cuni.mff.ufal.volk.Speech;
import cz.cuni.mff.ufal.volk.Text;
import cz.cuni.mff.ufal.volk.services.Services;
import cz.cuni.mff.ufal.volk.services.TextToSpeech;

/**
 * Simple user input listener. If the text on the input starts with the specified keyword,
 * speech is generated and sent to all registered Nabaztags.
 *
 * @author Bartlomiej Etenkowski
 */
public class ReaderInputProcessor implements Listener<String> {

	private static final Logger log = Logger.getLogger(ReaderInputProcessor.class);

	public ReaderInputProcessor(NabaztagController nab, String keyword) {
		nab.getClass();
		keyword.getClass();
		this.controller = nab;
		this.keyword = keyword;
	}

	private final NabaztagController controller;
	private final String keyword;

	@Override
  public void process(String event) {
		log.trace(String.format("process(event=%s)", event.replace("\r", "\\r").replace("\n", "\\n")));
	  if (event.startsWith(keyword)) {
	  	String text = event.substring(keyword.length());
	  	TextToSpeech tts = (TextToSpeech)Services.registered().get("volk.tts");
	  	Speech s = tts.process(new Text("cs", text), null);
	  	for (NabaztagProxy nab : controller.getRegisteredBunnies())
	  		nab.playSound(s);
	  }
  }

}
